package com.wonderful.eyepetizer.ui.pages.detail.viewmodel

import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.viewModelScope
import com.wonderful.eyepetizer.app.base.BaseViewModel
import com.wonderful.eyepetizer.data.entity.bean.Item
import com.wonderful.eyepetizer.data.entity.bean.ItemData
import com.wonderful.eyepetizer.data.repo.MainRepo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/1/30 11:20
 */
@HiltViewModel
class VideoDetailViewModel @Inject constructor(private val mRepo: MainRepo) : BaseViewModel() {
    val itemList = mutableStateListOf<Item>()
    var refreshing = mutableStateOf(false)

    fun getRelateVideoList(videoId: Int) {
        viewModelScope.launch {
            refreshing.value = true
            mRepo.getRelateVideoList(videoId).data?.itemList?.let { items ->
                if (items.isNotEmpty()) {
                    itemList.clear()
                    itemList.addAll(items)
                }
            }
            refreshing.value = false
        }
    }

    fun saveVideo(itemData: ItemData) {
        viewModelScope.launch {
            mRepo.saveVideo(itemData)
        }
    }
}